package com.walle.bio;

import java.io.*;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author zhangjiapeng
 * @Package com.walle.bio
 * @Description: ${todo}
 * @date 2019/1/18 13:47
 */
public class TimeServerHandler implements Runnable {

    private Socket socket;

    public TimeServerHandler(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);
            String buff = reader.readLine();
            System.out.println("time server received msg" + buff);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String rtn = "current time is " + sdf.format(new Date());
            writer.write(rtn);
            writer.flush();
            socket.shutdownOutput();
            reader.close();
            reader = null;
            writer.close();
            writer = null;

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
